package com.ibeeking.found.cop.b.rest.service.mq;

import com.ibeeking.found.common.entity.MqMsgEventParam;
import com.ibeeking.found.cop.api.common.param.MsgEventDataTalkParam;
import com.ibeeking.found.cop.api.common.param.MsgEventDepartmentParam;
import com.ibeeking.found.cop.b.rest.service.ICopDepartmentService;
import com.ibeeking.found.cop.b.rest.service.IDataTalkService;
import com.ibeeking.found.cop.b.rest.service.IWxOpenService;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class CopDataTalkMqMsgEventHandler {

    @Resource
    private IDataTalkService dataTalkServiceImpl;

    @Resource
    private IWxOpenService wxOpenServiceImpl;

    public boolean auditApproveMsgEvent(MqMsgEventParam mqParam, Map<String, Object> extMap) {
        try {
            LogUtils.info(">>>>>>> auditApproveMsgEvent:{}", JsonUtils.toJsonStr(mqParam));
            String tenantId = mqParam.getTenantId();
            String paramStr = mqParam.getParams();
            if(StringUtils.isEmpty(paramStr)) {
                LogUtils.error("从MQ读取参数是空的！");
                return false;
            }
            MsgEventDataTalkParam dataTalkParam = JsonUtils.fromJson(paramStr, MsgEventDataTalkParam.class);
            String employeeCode = dataTalkParam.getEmployeeCode();
            String friendCode = dataTalkParam.getFriendsCode();
            String welcomeCode = dataTalkParam.getWelcomeCode();
            dataTalkServiceImpl.updateFriendsAgree(employeeCode, friendCode, welcomeCode);
            return true;
        } catch (Exception e) {
            LogUtils.error("同步信息任务处理异常:{}", e);
            return false;
        }
    }

    public boolean notifySyncMsgEvent(MqMsgEventParam mqParam, Map<String, Object> extMap) {
        try {
            LogUtils.info(">>>>>>> notifySyncMsgEvent:{}", JsonUtils.toJsonStr(mqParam));
            String tenantId = mqParam.getTenantId();
            //同步部门列表
            String paramStr = mqParam.getParams();
            if(StringUtils.isEmpty(paramStr)) {
                LogUtils.error("从MQ读取参数是空的！");
                return false;
            }
            MsgEventDataTalkParam dataTalkParam = JsonUtils.fromJson(paramStr, MsgEventDataTalkParam.class);
            String agentId = dataTalkParam.getAgentID();
            dataTalkServiceImpl.syncBatchDataTalkFromWxCp(Long.valueOf(tenantId));
            return true;
        } catch (Exception e) {
            LogUtils.error("同步信息任务处理异常:{}", e);
            return false;
        }
    }

}

